<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="float-right">
        <el-button
          type="primary"
          @click="addRoleAlert()"
        >添加群组</el-button>
      </div>
    </div>
    <div class="table_container">
      <el-table
        border
        v-loading="tableloading"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="#"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="role"
          label="群组"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="count"
          label="群组人员数"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="editorRole(scope.row)"
              size="small"
            >编辑权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      title="添加群组"
      :visible.sync="dialogFormVisible"
      width="30%"
      center
    >
      <el-form
        :model="addRoleData"
        :rules="rules"
        ref="addRoleData"
      >
        <el-form-item
          label="群组名"
          :label-width="formLabelWidth"
          prop="role"
        >
          <el-input v-model="addRoleData.role"></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addRoleForm('addRoleData')"
        >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { fetchAddRole, fetchSysRoleData } from "@/api/table";
export default {
  data() {
    return {
      tableData: [],
      sysUserList: [],
      limit: 10,
      currentPage: 1,
      total: 0,
      dialogFormVisible: false,
      formLabelWidth: "80px",
      addRoleData: {
        role: ""
      },
      rules: {
        role: [{ required: true, message: "请输入群组名称", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getSysRoleData();
  },
  methods: {
    addRoleAlert() {
      this.dialogFormVisible = true;
    },
    addRoleForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.addRole();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    async addRole() {
      const adduser = await fetchAddRole({
        role: this.addRoleData.role
      });
      this.$message({
        type: "success",
        message: "添加成功!"
      });
      this.getSysRoleData();
    },
    async getSysRoleData() {
      this.tableloading = true;
      const SysRoleData = await fetchSysRoleData();
      this.tableData = [];
      this.tableloading = false;
      SysRoleData.data.forEach(role => {
        const option = {};
        option.role = role.role;
        option.id = role.id;
        option.count = role.count;
        this.tableData.push(option);
      });
    },
    editorRole(e) {
      this.$router.push({
        path: "/teamManage/accessManage/editorAccessManage",
        query: {
          roleId: e.id,
          name: e.role
        }
      });
    }
  }
};
</script>
